//
//  TrackViewController.m
//  CoolTennisBall
//
//  Created by CA on 16/3/28.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "TrackVideoViewController.h"
#import "TrackVideoCell.h"
#import "TrackVideo.h"
#import "CommunicationManager.h"
#import "FUIAlertView+customizeAppearance.h"
#import "AuthorizationStatus.h"
#import "RecordVideoViewController.h"
#import "User.h"
#import "PlayVideoViewController.h"
#import "UIImage+YFExtension.h"
#import "SearchViewController.h"

static const CGFloat kPlayVideoAspect = 130.0f/145;
static const CGFloat kVideoMargin = 10.0f;
static const CGFloat kAnimationDuration = 0.35f;
static const CGFloat kBottomBarHeight = 44.0f;

static NSString * const kTrackRecordSegue = @"TrackRecordSegue";
static NSString * const kPlayVideoSegue = @"PlayVideoSegue";
static NSString * const kDemoVideoPathUrl = @"http://tennis-10015299.video.myqcloud.com/tennis_example.mp4";

@interface TrackVideoViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, TrackVideoCellDelegate, UITextFieldDelegate, FUIAlertViewDelegate, SearchViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UIBarButtonItem *editItem;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *trashItem;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomBarHeightConstraint;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *backItem;
@property (nonatomic, strong) User *user;

@property (nonatomic, strong) NSMutableArray *trackVideos;

@end

@implementation TrackVideoViewController
{
    BOOL _isEditing;
    SearchViewController *_searchViewController;
    YFLanguage _currentLanguage;
}

#pragma mark - Getter/Setter

- (User *)user
{
    if (_user == nil) {
        _user = [User getLastUser];
    }
    return _user;
}
- (NSMutableArray *)trackVideos {
    if (_trackVideos == nil) {
        _trackVideos = [NSMutableArray array];
        
    }
    return _trackVideos;
}

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    _currentLanguage = [NSBundle currentLangeuage];
    for (TrackVideo *video in self.user.trackVideos) {
        [self.trackVideos addObject:video];
        [video videoImage];
    }
    [self configureLocalization];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.collectionView reloadData];
}


- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    if (_isEditing) {
        [[RLMRealm defaultRealm] beginWriteTransaction];
        for (TrackVideo *video in self.trackVideos) {
            video.isSelected = NO;
        }
        [[RLMRealm defaultRealm] commitWriteTransaction];
    }
}

#pragma mark - Localization

- (void)configureLocalization
{
    self.title = NSLocalizedString(@"Live Video", nil);
    self.trashItem.title = NSLocalizedString(@"Delete", nil);
    self.editItem.title = NSLocalizedString(@"Select", nil);
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:kTrackRecordSegue]) {
        RecordVideoViewController *controller = segue.destinationViewController;
        
        __weak typeof(self) weakSelf = self;
        
        controller.videoBlock = ^(TrackVideo *video){
            User *user = [User getLastUser];
            
            [[RLMRealm defaultRealm] beginWriteTransaction];
            video.name = [NSString stringWithFormat:@"Video %zd", [user.trackVideos count] + 1];
            video.imagePath = video.imagePath;
            [user.trackVideos addObject:video];
            [[RLMRealm defaultRealm] commitWriteTransaction];
            [weakSelf updateDataSource];
        };
        
    } else if ([segue.identifier isEqualToString:kPlayVideoSegue]) {
        PlayVideoViewController *controller = segue.destinationViewController;
        if ([sender isKindOfClass:[NSString class]]) {
            controller.networkUrlPath = sender;
        } else {
            controller.video = sender;
        }
    }
}

- (void)updateDataSource {
    [self.trackVideos removeAllObjects];
    
    for (TrackVideo *video in [User getLastUser].trackVideos) {
        [self.trackVideos addObject:video];
    }
    [self.collectionView reloadData];
}

#pragma mark - IBAction

- (IBAction)editVideos:(UIBarButtonItem *)sender {
    self.trashItem.enabled = NO;
    
    if (!_isEditing) {
        [self showEditBottomBar];
        self.collectionView.allowsMultipleSelection = YES;
        self.editItem.title = NSLocalizedString(@"Cancel", nil);
        self.backItem.title = NSLocalizedString(@"Select All", nil);
        self.backItem.image = nil;
        
        if (_currentLanguage == kLanguageFrennch) {
            self.title = @"";
        }
        
    } else {
        [self hideEditBottomBar];
        self.collectionView.allowsMultipleSelection = NO;
        self.editItem.title = NSLocalizedString(@"Select", nil);
        [self.backItem setImage:[UIImage imageNamed:@"sport_icon_blackbackArrow"]];
        self.backItem.title = nil;
        if (_currentLanguage == kLanguageFrennch) {
            self.title = NSLocalizedString(@"Live Video", nil);
        }
        if ([self.user.trackVideos count] > 0) {
            
            [[RLMRealm defaultRealm] beginWriteTransaction];
//            for (TrackVideo *video in self.user.trackVideos) {
//                video.isSelected = NO;
//            }
            for (TrackVideo *video in self.trackVideos) {
                video.isSelected = NO;
            }
            [[RLMRealm defaultRealm] commitWriteTransaction];
        }
    }
    
    _isEditing = !_isEditing;
    [self.collectionView reloadData];
}

- (IBAction)trashVideoItems:(id)sender {

    NSMutableArray *deleteItemIndexPaths = [@[] mutableCopy];
    
    for (NSInteger idx = 0; idx < [self.user.trackVideos count]; idx++) {
        TrackVideo *video = self.user.trackVideos[idx];
        if (video.isSelected) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
            [deleteItemIndexPaths addObject:indexPath];
        }
    }
    
    
    FUIAlertView *alertView = [[FUIAlertView alloc] initWithTitle:NSLocalizedString(@"Confirm to Delete？", nil) message:NSLocalizedString(@"The video cannot be recovered once deleted.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Yes", nil), nil];
    [alertView customizeAppearance];
    [alertView show];
    __weak typeof(self) weakSelf = self;
    alertView.onOkAction = ^{
        RLMRealm *realm = [RLMRealm defaultRealm];
        [realm beginWriteTransaction];
        for (TrackVideo *video in self.user.trackVideos) {
            if (video.isSelected) {
                // 删除本地的照片
                [video removeVideoImage];
                [video removeVideo];
                [realm deleteObject:video];
            }
        }
        [realm commitWriteTransaction];
        
//        [self.collectionView deleteItemsAtIndexPaths:deleteItemIndexPaths];
        self.trashItem.enabled = NO;
        [weakSelf updateDataSource];
    };
    
}

- (IBAction)backOrAllSelected:(UIBarButtonItem *)sender {
    
    if (_isEditing) {
        
        BOOL allVideoItemSelected = [self hasAllOfVideoItemsSelected];
        if (allVideoItemSelected) {
            self.backItem.title = NSLocalizedString(@"Select All", nil);
        } else {
            self.backItem.title = NSLocalizedString(@"Deselect All", nil);
        }
        
        [[RLMRealm defaultRealm] beginWriteTransaction];
//        for (TrackVideo *video in self.user.trackVideos) {
//            video.isSelected = !allVideoItemSelected;
//        }
        for (TrackVideo *video in self.trackVideos) {
            video.isSelected = !allVideoItemSelected;
        }
        [[RLMRealm defaultRealm] commitWriteTransaction];
        
        [self.collectionView reloadData];
        
        self.trashItem.enabled = [self hasItemSelected] > 0;
        
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

#pragma mark - Private Method

- (BOOL)hasAllOfVideoItemsSelected
{
    // 是否全部被选择
    BOOL hasAllVideoItemsSelected = YES;
    
//    for (TrackVideo *video in self.user.trackVideos) {
//        if (!video.isSelected) {
//            hasAllVideoItemsSelected = NO;
//        }
//    }
    for (TrackVideo *video in self.trackVideos) {
        if (!video.isSelected) {
            hasAllVideoItemsSelected = NO;
        }
    }
    
    return hasAllVideoItemsSelected;
}

- (void)showEditBottomBar
{
    if (self.bottomBarHeightConstraint.constant < kBottomBarHeight) {
        
        [UIView animateWithDuration:kAnimationDuration delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.bottomBarHeightConstraint.constant = kBottomBarHeight;
            [self.view layoutIfNeeded];
        } completion:nil];
    }
}

- (void)hideEditBottomBar
{
    if (self.bottomBarHeightConstraint.constant > 0) {
        [UIView animateWithDuration:kAnimationDuration delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.bottomBarHeightConstraint.constant = 0;
            [self.view layoutIfNeeded];
        } completion:nil];
    }

}

/**
 *  显示搜索设备
 */
- (void)showSearchDevicesView
{
    if (![CommunicationManager shareInstance].isCentralReady) {
        return;
    }
    
    if (_searchViewController == nil || _searchViewController.parentViewController == nil) {
        
        [[CommunicationManager shareInstance] stopScanningPeripherals];
        
        SearchViewController *controller = [[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil];
        controller.delegate = self;
        
        [controller presentInParentViewController:self.tabBarController animationType:DetailViewControllerPresentAnimationTypeFade];
        _searchViewController = controller;
    }
    
}

//设置时间戳校准
- (void)configureTimeStampSetting
{
    [[CommunicationManager shareInstance] performOperationType:OperationTypeVerify object:nil completionBlock:^(BOOL success, id feedbackData) {
        if (success) {
            [BTHandler handleTimeStampVerifyData:feedbackData withCompletion:^(BOOL successful) {
                if (successful) {
                    NSLog(@"时间校正成功");
                    [self configureHandStyle];
                }
            }];
        }
    }];
}

//连接成功后设置左右手
- (void)configureHandStyle
{
    User *user = [User getLastUser];
    [[CommunicationManager shareInstance] performOperationType:OperationTypeHand object:[NSString stringWithFormat:@"%d", (int)user.userInforItem.hand] completionBlock:^(BOOL success, id feedbackData) {
        if (success) {
            [BTHandler handleHandData:feedbackData withCompletion:^(BOOL success, NSString *hand) {
                if (success) {
                    if ([AuthorizationStatus checkCameraAuthorizationStatus]) {
                        [self performSegueWithIdentifier:kTrackRecordSegue sender:nil];
                    }
                    NSLog(@"设置左右手成功");
                }
            }];
        }
    }];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_isEditing) {
        return [self.user.trackVideos count];
    }
    if (self.user.userDevice.oemType == OemTypeOrignal){
        
        return [self.user.trackVideos count] + 2;
    }else {
        // 连接的设备是海德或TS，隐藏实例视频
        return [self.user.trackVideos count] + 1;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isEditing) {
        TrackVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlayVideoCell" forIndexPath:indexPath];
        cell.delegate = self;
        
//        TrackVideo *video = self.user.trackVideos[indexPath.row];
        TrackVideo *video = self.trackVideos[indexPath.row];
        
        [cell configureWithTrackVideo:video isEditting:_isEditing indexPath:indexPath];
        
        return cell;
        
    } else {
        
        switch (self.user.userDevice.oemType) {
            case OemTypeOrignal:{
                if (indexPath.row == [self.user.trackVideos count] + 1) {
                    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddVideoCell" forIndexPath:indexPath];
                    
                    return cell;
                }
                TrackVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlayVideoCell" forIndexPath:indexPath];
                cell.delegate = self;
                
                if (indexPath.row == 0) {
                    cell.nameLabel.text = NSLocalizedString(@"Demo video", nil);
                    cell.prevideImageView.image = [UIImage imageNamed:@"track_demo_bg"];
//                  [cell configureWithTrackVideo:nil isEditting:_isEditing indexPath:indexPath];
                } else {
                    //TrackVideo *video = self.user.trackVideos[indexPath.row - 1];
                    TrackVideo *video = self.trackVideos[indexPath.row - 1];
                    [cell configureWithTrackVideo:video isEditting:_isEditing indexPath:indexPath];
                }
                return cell;
            }
                break;
            case OemTypeHD:{
                if (indexPath.row == [self.user.trackVideos count]) {
                    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddVideoCell" forIndexPath:indexPath];
                    
                    return cell;
                }
                TrackVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlayVideoCell" forIndexPath:indexPath];
                cell.delegate = self;
                
                //                TrackVideo *video = self.user.trackVideos[indexPath.row];
                TrackVideo *video = self.trackVideos[indexPath.row];
                [cell configureWithTrackVideo:video isEditting:_isEditing indexPath:indexPath];
                return cell;
            }
                break;
            case OemTypeTS:{
                if (indexPath.row == [self.user.trackVideos count]) {
                    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddVideoCell" forIndexPath:indexPath];
                    
                    return cell;
                }
                TrackVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlayVideoCell" forIndexPath:indexPath];
                cell.delegate = self;
                
               // TrackVideo *video = self.user.trackVideos[indexPath.row];
                TrackVideo *video = self.trackVideos[indexPath.row];
                [cell configureWithTrackVideo:video isEditting:_isEditing indexPath:indexPath];
                return cell;
            }
                break;
            case OemTypeT5:{
                if (indexPath.row == [self.user.trackVideos count]) {
                    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddVideoCell" forIndexPath:indexPath];
                    
                    return cell;
                }
                TrackVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlayVideoCell" forIndexPath:indexPath];
                cell.delegate = self;
                
                // TrackVideo *video = self.user.trackVideos[indexPath.row];
                TrackVideo *video = self.trackVideos[indexPath.row];
                [cell configureWithTrackVideo:video isEditting:_isEditing indexPath:indexPath];
                return cell;
            }
                break;
                
            default:
                break;
        }
        
    }
}



#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Select item at indexPath: %@",indexPath);
    
    if (_isEditing) {
        if (indexPath.row < [self.user.trackVideos count]) {
//            TrackVideo *video = self.user.trackVideos[indexPath.row];
            TrackVideo *video = self.trackVideos[indexPath.row];
            [video toggleChecked];
            TrackVideoCell *cell = (TrackVideoCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
            [cell configureWithTrackVideo:video isEditting:_isEditing indexPath:indexPath];
        }
        
        self.trashItem.enabled = [self hasItemSelected] > 0;
        
        if ([self hasAllOfVideoItemsSelected]) {
            self.backItem.title = NSLocalizedString(@"Deselect All", nil);
        } else {
            self.backItem.title = NSLocalizedString(@"Select All", nil);
        }

    }else {
        switch (self.user.userDevice.oemType) {
            case OemTypeOrignal: {
                if (indexPath.row == [self.user.trackVideos count] + 1) {
                    [self judgeIsPeripheralConnectedWithoutAlertView];
                    return;
                }else if (indexPath.row == 0){
                    [self playExamplesVideo];
                }else {
                    //TrackVideo *video = self.user.trackVideos[indexPath.row - 1];
                    TrackVideo *video = self.trackVideos[indexPath.row - 1];
                    [self performSegueWithIdentifier:kPlayVideoSegue sender:video];
                }
            }
                
                break;
            case OemTypeHD: {
                if (indexPath.row == [self.user.trackVideos count]) {
                    [self judgeIsPeripheralConnectedWithoutAlertView];
                    return;
                }else {
                    //TrackVideo *video = self.user.trackVideos[indexPath.row];
                    TrackVideo *video = self.trackVideos[indexPath.row];
                    [self performSegueWithIdentifier:kPlayVideoSegue sender:video];
                }
            }
                break;
            case OemTypeTS: {
                if (indexPath.row == [self.user.trackVideos count]) {
                    [self judgeIsPeripheralConnectedWithoutAlertView];
                    return;
                }else {
                    // TrackVideo *video = self.user.trackVideos[indexPath.row];
                     TrackVideo *video = self.trackVideos[indexPath.row];
                    [self performSegueWithIdentifier:kPlayVideoSegue sender:video];
                }
            }
                
                break;
            case OemTypeT5:{
                if (indexPath.row == [self.user.trackVideos count]) {
                    [self judgeIsPeripheralConnectedWithoutAlertView];
                    return;
                }else {
                    // TrackVideo *video = self.user.trackVideos[indexPath.row];
                    TrackVideo *video = self.trackVideos[indexPath.row];
                    [self performSegueWithIdentifier:kPlayVideoSegue sender:video];
                }
            }
                break;
        }
    }
    
}

- (void)judgeIsPeripheralConnectedWithoutAlertView {
    if ([CommunicationManager shareInstance].isPeripheralConnectedWithoutAlertView) {
        if ([AuthorizationStatus checkCameraAuthorizationStatus]) {
            [self performSegueWithIdentifier:kTrackRecordSegue sender:nil];
        }
    } else {
        FUIAlertView *alertView = [[FUIAlertView alloc] initWithTitle:NSLocalizedString(@"Please connect your device to record live video", nil) message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Yes", nil) otherButtonTitles:nil, nil];
        [alertView customizeAppearance];
        alertView.onCancelAction = ^{
            [self showSearchDevicesView];
        };
        
        [alertView show];
        
    }
}

- (void)playExamplesVideo {
    
    if ([YFCheckNetworkStatus shareInstance].networkStatus == AFNetworkReachabilityStatusReachableViaWiFi) {
        [self performSegueWithIdentifier:kPlayVideoSegue sender:kDemoVideoPathUrl];
    }else {
        FUIAlertView *alertView = [[FUIAlertView alloc] initWithTitle:NSLocalizedString(@"Reminder", nil) message:NSLocalizedString(@"Viewing this video under cellular network might result in high data usage.   Proceed / Cancel?", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Proceed", nil), nil];
        [alertView customizeAppearance];
        
        alertView.onOkAction = ^{
            [self performSegueWithIdentifier:kPlayVideoSegue sender:kDemoVideoPathUrl];
        };
        [alertView show];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Deselect item at indexPath: %@",indexPath);
    
    if (_isEditing) {
        if (indexPath.row < [self.user.trackVideos count]) {
//            TrackVideo *video = self.user.trackVideos[indexPath.row];
            TrackVideo *video = self.trackVideos[indexPath.row];
            [video toggleChecked];
            TrackVideoCell *cell = (TrackVideoCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
            [cell configureWithTrackVideo:video isEditting:_isEditing indexPath:indexPath];
        }
        
        self.trashItem.enabled = [self hasItemSelected] > 0;
        
        if ([self hasAllOfVideoItemsSelected]) {
            self.backItem.title = NSLocalizedString(@"Deselect All", nil);
        } else {
            self.backItem.title = NSLocalizedString(@"Select All", nil);
        }
    }
}

- (BOOL)hasItemSelected
{
//    if ([self.user.trackVideos count] > 0) {
//        for (TrackVideo *video in self.user.trackVideos) {
//            if (video.isSelected) {
//                return YES;
//            }
//        }
//    }
    if ([self.trackVideos count] > 0) {
        for (TrackVideo *video in self.trackVideos) {
            if (video.isSelected) {
                return YES;
            }
        }
    }
    return NO;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (self.view.bounds.size.width - kVideoMargin * 3)/2;
    return CGSizeMake(width, width * kPlayVideoAspect);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(kVideoMargin, kVideoMargin, kVideoMargin, kVideoMargin);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark - TrackVideoCellDelegate
- (void)trackVideoCellDidTouchEditButton:(TrackVideoCell *)cell
{
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    
    FUIAlertView *alertView = [[FUIAlertView alloc] initWithTitle:NSLocalizedString(@"Rename the Video", nil) message:nil delegate:self cancelButtonTitle: NSLocalizedString(@"No", nil) otherButtonTitles:NSLocalizedString(@"Yes", nil), nil];
    alertView.alertViewStyle = FUIAlertViewStylePlainTextInput;
    [alertView customizeAppearance];
    [alertView show];
    
    FUITextField *textField = [alertView textFieldAtIndex:0];
    textField.delegate = self;
    textField.returnKeyType = UIReturnKeyDone;
    textField.textFieldColor = [UIColor whiteColor];
    textField.placeholder = NSLocalizedString(@"Track.PleaseEnter", nil);
    
    alertView.onOkAction = ^{
        //TrackVideo *video = self.user.trackVideos[indexPath.row];
        TrackVideo *video = self.trackVideos[indexPath.row];
        [[RLMRealm defaultRealm] beginWriteTransaction];
        video.name = textField.text;
        [[RLMRealm defaultRealm] commitWriteTransaction];
        
        TrackVideoCell *cell = (TrackVideoCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
        [cell configureWithTrackVideo:video isEditting:_isEditing indexPath:indexPath];
    };
}

- (void)didPresentAlertView:(FUIAlertView *)alertView
{
    FUITextField *textField = [alertView textFieldAtIndex:0];
    [textField becomeFirstResponder];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - SearchViewControllerDelegate

- (void)searchViewControllerDidConnectPeripheral:(SearchViewController *)controller
{
    [self configureTimeStampSetting];
}


@end
